package com.leetcode.August;

import javax.swing.tree.TreeNode;

/**
 * @Description: 437. 路径总和 III
 * @Author: Mr.Fxy
 * @CreateTime: 2024-08-09 11:27
 */
public class LC437路径总和III {
    int count=0;
    public int pathSum(TreeNode root, int targetSum) {
        if (root==null)return 0;
        Sum(root,targetSum);
        pathSum(root.left,targetSum);
        pathSum(root.right,targetSum);
        return count;
    }
    public void Sum(TreeNode root, long sum){
        if (root==null)return;
        sum-=root.val;
        if (sum==0)count++;
        Sum(root.left,sum);
        Sum(root.right,sum);
    }

}
/**
 * java 双重递归 思路：首先先序递归遍历每个节点，再以每个节点作为起始点递归寻找满足条件的路径。
 * [1000000000,1000000000,null,294967296,null,1000000000,null,1000000000,null,1000000000] 调用递归的时候传入的参数是targetSum-root.val，
 * 这里root.val太大了，递归调用多了targetSum-root.val就会溢出整数型的最小值。把参数类型换成long即可。
 * */
